import random

from .base_layer import BaseLayer
from . import utils

FULL_RANDOM = False  # 通过此变量来确定信息战是完全随机，还是有一点策略性


class NetworkAttackLayer(BaseLayer):
    def __init__(self):
        self.color = None

    def setup(self, color):
        self.color = color

    def step(self):
        action_list = None
        if FULL_RANDOM:
            action_list = self.random_strategy()
        else:
            action_list = self.with_brain_strategy()
        return action_list

    def random_strategy(self):
        """
        完全随机的作战风格
        """
        has_choose = {
            401: False,
            402: False,
            403: False,
            404: False
        }
        action_list = []
        current_attack_num = utils.get_current_attack_num()
        # 循环执行0～4次
        for _ in range(random.randint(0, 4)):
            # 随机选择一个动作进行尝试生成
            random_number = random.randint(1, 4)
            if random_number == 1:
                # 1、尝试进行401-伪装接入
                if has_choose[401] == True:
                    continue
                action = self.gen_disguise_access()
                if action and utils.action_valid(action):
                    current_attack_num["red" if self.color == 0 else "blue"] += 1
                    action_list.append(action)
                    has_choose[401] = True
            elif random_number == 2:
                # 2、尝试进行402-病毒植入
                if has_choose[402] == True:
                    continue
                action = self.gen_implant_virus()
                if action and utils.action_valid(action):
                    current_attack_num["red" if self.color == 0 else "blue"] += 1
                    action_list.append(action)
                    has_choose[402] = True
            elif random_number == 3:
                # 3、尝试进行403-信息窃取
                if has_choose[403] == True:
                    continue
                action = self.gen_steal_infos()
                if action and utils.action_valid(action):
                    current_attack_num["red" if self.color == 0 else "blue"] += 1
                    action_list.append(action)
                    has_choose[403] = True
            elif random_number == 4:
                # 3、尝试进行404-身份凭证伪造
                if has_choose[404] == True:
                    continue
                action = self.gen_identity_credential_forgery()
                if action and utils.action_valid(action):
                    current_attack_num["red" if self.color == 0 else "blue"] += 1
                    action_list.append(action)
                    has_choose[404] = True
        utils.set_current_attack_num(current_attack_num)
        return action_list

    def with_brain_strategy(self):
        """
        带点脑子的作战风格
        """
        has_choose = {
            401: False,
            402: False,
            403: False,
            404: False
        }
        action_list = []
        current_attack_num = utils.get_current_attack_num()
        # 循环执行0～4次
        for _ in range(random.randint(0, 4)):
            # 随机选择一个动作进行尝试生成
            if has_choose[401] == True:
                continue
            random_number = random.randint(1, 6)
            if random_number == 1 or random_number == 2 or random_number == 3:  # 可能性更大
                # 1、尝试进行401-伪装接入
                action = self.gen_disguise_access()
                if action and utils.action_valid(action):
                    current_attack_num["red" if self.color == 0 else "blue"] += 1
                    action_list.append(action)
                    has_choose[401] = True
            elif random_number == 4:
                # 2、尝试进行402-病毒植入
                if has_choose[402] == True:
                    continue
                action = self.gen_implant_virus()
                if action and utils.action_valid(action):
                    current_attack_num["red" if self.color == 0 else "blue"] += 1
                    action_list.append(action)
                    has_choose[402] = True
            elif random_number == 5:
                # 3、尝试进行403-信息窃取
                if has_choose[403] == True:
                    continue
                action = self.gen_steal_infos()
                if action and utils.action_valid(action):
                    current_attack_num["red" if self.color == 0 else "blue"] += 1
                    action_list.append(action)
                    has_choose[403] = True
            elif random_number == 6:
                pass
        # 伪装接入：较大的随机数 or 无算子被伪装接入（两个条件或），由于上面已经考虑过随机数较大的情况了，所以此处使用与排除这种情况
        if has_choose[401] == False and len(utils.get_peer_alive_has_disguised_access_ids(self.color)) > 0:
            action = self.gen_disguise_access()
            if action and utils.action_valid(action):
                current_attack_num["red" if self.color == 0 else "blue"] += 1
                action_list.append(action)
        # 身份伪造：上一时刻伪造接入成功率低于阈值（0.5）
        accdisguised_access_success_rete = round(utils.ACTION_SUCC_RATE[401][0], 2) if self.color == 0 else round(
            utils.ACTION_SUCC_RATE[401][1], 2)
        if accdisguised_access_success_rete <= 0.5:
            action = self.gen_identity_credential_forgery()
            if action and utils.action_valid(action):
                current_attack_num["red" if self.color == 0 else "blue"] += 1
                action_list.append(action)
        utils.set_current_attack_num(current_attack_num)
        return action_list

    def gen_disguise_access(self):
        # 生成动作
        action = {
            "color": self.color,
            "type": 401,
            "remain_time": utils.ACTION_COST_TIME[401]
        }
        return action

    def gen_implant_virus(self):
        # 1、获取已成功伪装接入的对方未死亡算子的id构成的列表
        has_disguised_access_ids = utils.get_peer_alive_has_disguised_access_ids(self.color)
        # 2、随机取出一个活着的算子id作为攻击对象，如果算子都死亡了则直接返回
        if len(has_disguised_access_ids) == 0:
            return None
        ops_id = random.choice(has_disguised_access_ids)
        # 3、生成动作
        action = {
            "color": self.color,
            "type": 402,
            "ops_id": ops_id,
            "remain_time": utils.ACTION_COST_TIME[402]
        }
        return action

    def gen_steal_infos(self):
        # 1、获取已成功伪装接入的对方未死亡算子的id构成的列表
        has_disguised_access_ids = utils.get_peer_alive_has_disguised_access_ids(self.color)
        # 2、随机取出一个活着的算子id作为攻击对象，如果算子都死亡了则直接返回
        if len(has_disguised_access_ids) == 0:
            return None
        ops_id = random.choice(has_disguised_access_ids)
        # 3、生成动作
        action = {
            "color": self.color,
            "type": 403,
            "ops_id": ops_id,
            "remain_time": utils.ACTION_COST_TIME[403]
        }
        return action

    def gen_identity_credential_forgery(self):
        # 生成动作
        action = {
            "color": self.color,
            "type": 404,
            "remain_time": utils.ACTION_COST_TIME[404]
        }
        return action

    def reset(self):
        pass
